package mytest.leetcode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class Hot3 {

    public static void main(String[] args) {
        int[] nums = {0,3,7,2,5,8,4,6,0,1};
        int maxLength = getMaxLength(nums);
        int maxLength2 = getMaxLength_max(nums);
        System.out.println(maxLength);
        System.out.println(maxLength2);
    }

    //优化版
    public static int getMaxLength_max(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        HashSet<Integer> numSet = new HashSet<>();
        for (int num : nums) {
            numSet.add(num);
        }
        int maxLength = 0;
        for (Integer num : numSet) {
            int count = 0;
            if ( !numSet.contains(num - 1)){
                while (numSet.contains(num + 1)) {
                    count ++;
                    num ++;
                }
            }
            maxLength = Math.max(maxLength, count);
        }

        return maxLength + 1;
    }


    //暴力超时版
    public static int getMaxLength(int[] nums) {
        int numsLength = nums.length;
        if (numsLength == 0) {
            return 0;
        }
        int maxLength = 0;
        List<Integer> list = new ArrayList<>();
        for (int num : nums) {
            list.add(num);
        }
        for (int i = 0; i < numsLength; i++) {
            int count = 0;
            int num = nums[i];
            while (list.contains(num + 1)) {
                count++;
                num = num + 1;
            }
            maxLength = Math.max(maxLength, count);
        }
        return maxLength + 1;
    }
}
